<?php

class Shop extends CI_Controller{
    
    public function index($success_message2 = null){    
        $this->load->model('Document_types_model');
        $this->load->model('Company_model');
       
        $vars['typeDocuments'] = $this->Document_types_model->select_guide_shop();
        $vars['company'] = $this->Company_model->getAll();
        $vars['parametro'] = true;
        $vars['searchsales'] = true;
        if (isset($success_message2)) {
            $vars['success_message2'] = true;
        }
        $vars['content_view'] = 'shop_form';
        $vars['menu'] = 'shop';
         
        $this->load->view('template', $vars);
         
    }
    
     public function search($edit = null) {

        $this->load->model('Shop_model');
        $this->load->model('Document_types_model');
        $this->load->model('Company_model');

        $document_type_id = $this->input->post('tipo');
        $document_code = $this->input->post('numero');

        $vars['typeDocuments'] = $this->Document_types_model->select_guide_sale();
        $vars['company'] = $this->Company_model->getAll();

        $datesale = $this->Shop_model->search_shop($document_code);
        $saledetails = $this->Shop_model->search_shop_deatils($document_code);
       if (isset($edit)) {
            $vars['success_message'] = true;
        }
        $vars['fecha'] =  date('d/m/Y');
        $vars['saledetails'] = $saledetails;
        $vars['datesale'] = $datesale;
        $vars['contandodatos'] = count($datesale);        
        $vars['searchsales'] = true;
        $vars['content_view'] = 'shop_form';
        $vars['menu'] = 'shop';
        $this->load->view('template', $vars);
        
    }
    
     public function add($success_message = null) {
        $this->load->model('Company_model');
        
        $this->load->model('Document_types_model');
        $vars['typeDocuments'] = $this->Document_types_model->select_guide_shop();
        
        if (isset($success_message)) {
            $vars['success_message'] = true;
        }
       $vars['company'] = $this->Company_model->getAll();
        $vars['content_view'] = 'shop_form';
        $vars['menu'] = 'shop';
 
        $this->load->view('template', $vars);
    }
    
    
    
    public function products() {
        $code = null;
        $description = null;
        $this->load->model('Products_model');
        if ($this->input->post('code')) {
            $code = $this->input->post('code');
        }
        if ($this->input->post('description')) {
            $description = $this->input->post('description');
        }
        $products = array();
        if ($code != null || $description != null) {
            $products = $this->Products_model->search($code, $description);
        }
        $vars['products'] = $products;
        $vars['code'] = $code;
        $vars['description'] = $description;
        $this->load->view('products_list_sale_shop', $vars);
    }
    
    
     public function shops() {


        $company=null;
        $code = null;
        $date = null;
        $obtservations=null;
       
       

        if ($this->input->post('document_code')) {
            $code = $this->input->post('document_code');
        }
        if ($this->input->post('date')) {
            $date = $this->input->post('date');
        }
        if($this->input->post('obtservations')){
            $obtservations= $this->input->post('obtservations');
        }
        $shop = array();
        if ($company != null || $code != null || $date != null|| $obtservations != null) {
            $shop = $this->Products_model->search($code, $description);
        }
         $this->load->model('Shop_model');
        $shop = $this->Shop_model->getAll();
        $vars['shop'] = $shop;
        $vars['id_company'] = $company;
        $vars['document_code'] = $code;
        $vars['date'] = $date;
        $vars['obtservations'] = $obtservations;
        $this->load->view('shop_search', $vars);
    }
    
     public function editx($document_code)
    {
        $this->load->model('Shop_model');
        $this->load->model('Product_groups_model');
        $vars['productGroups'] = $this->Product_groups_model->getAll();
        $vars['content_view'] = 'shop_form';
        $vars['menu'] = 'Shop';
        $this->load->view('template', $vars);
    }





     public function stocks() {
        $code = null;
        $description = null;
        if ($this->input->post('code')) {
            $code = $this->input->post('code');
        }
        if ($this->input->post('description')) {
            $description = $this->input->post('description');
        }
        
        $this->load->model('warehouses_model');
	    $warehouses = $this->warehouses_model->getAll();
        $subwarehouses = $this->warehouses_model->getAll2();

        $this->load->model('Warehouses_product_model');
        $this->load->model('Products_model');
        $listStock = $this->Warehouses_product_model->listStockByProduct($code);
        $productData = $this->Products_model->find($code);
        $vars['priceProduct'] = $productData->price;
        $vars['codeProduct'] = $code;
        $vars['descriptionProduct'] = $description;
        $vars['listStock'] = $listStock;

        $vars['warehouses'] = $warehouses;
        $vars['subwarehouses'] = $subwarehouses;
        $vars['productData'] = $productData;
        $this->load->view('stock_list_shop', $vars);
    }
	
	public function submit(){
            
       if(!($this->input->post('id_shop'))){
                  
           $this->load->model('Shop_search_model');
           $this->load->model('Shop_model');
           $this->load->model('Warehouses_product_model');
           $this->load->model('Shop_kardex_model');
           $contenaumentador = $this->input->post('contador_detalles');
           $datei = $this->input->post('date');
           $date = str_replace('/', '-', $datei);
           $datef = date("Y-m-d", strtotime($date) ); 
              
            $data = array('document_type_id' => 3,
                        'document_code' => $this->input->post('numero'),
                        'date' => $datef,
                        'id_supplier' =>  $this->input->post('supplier'),
                        'observations' => $this->input->post('observations'),
                        't_change' => $this->input->post('t_change'),
                        'igv' => $this->input->post('igv'),
                        'money' =>  $this->input->post('type_money'),
                        'utilidad' => $this->input->post('utilidad'),
                        'otros_costos' => $this->input->post('costo'),
                        'amount' => $this->input->post('subtotalgeneral')
                        );
       
           $this->Shop_model->add_shop($data);
       for($i = 1 ; $i <= $contenaumentador; $i++){
               
               $data = array('document_code' => $this->input->post('numero'),
                             'product_code' => $this->input->post('code_product_'.$i.''),
                             'box' => $this->input->post('total_cajas_'.$i.''),  
                             'pack' => $this->input->post('total_paquetes_'.$i.''),  
                             'unit' => $this->input->post('total_unidades_'.$i.''),  
                             'flete' => $this->input->post('total_flete_'.$i.''),  
                             'seguro' => $this->input->post('total_seguro_'.$i.''), 
                             'Ad_Val' => $this->input->post('total_adval_'.$i.''), 
                             'Val_Unit' => $this->input->post('total_price_'.$i.''), 
                             'amount' => $this->input->post('total_subtotalprice_'.$i.''), 
                            );
                $this->Shop_model->add_shop_dateils($data);                
       
               
            for ($j = 1; $j <= 7; $j++){
                
                 $code_product = $this->input->post('code_product_'.$i);
                 $code_almacen = $this->input->post('code_almacen_'.$j.'_fila_'.$i);
                 //echo "<script language='JavaScript'>alert('code_product.$code_product');</script>";
                // echo "<script language='JavaScript'>alert('code_almacen.$code_almacen');</script>";
                 $productos_en_alamcenes = $this->Shop_model->select_almacen_productos($code_product,$code_almacen);
                 $num = count($productos_en_alamcenes);
                   // echo "<script language='JavaScript'>alert('JavaScript.$num');</script>";
                   // echo "caja almacen".$j." fila ".$i.": ".$this->input->post('caja_almacen_'.$j.'_fila_'.$i)."<br>";
                   // echo "paquete almacen".$j." fila ".$i.": ".$this->input->post('paquete_almacen_'.$j.'_fila_'.$i)."<br>";
                  // echo "unidad almacen".$j." fila ".$i.": ".$this->input->post('unidad_almacen_'.$j.'_fila_'.$i)."<br><br><br>";
                
                $caja_almacen = $this->input->post('caja_almacen_'.$j.'_fila_'.$i);
                $paquete_almacen = $this->input->post('paquete_almacen_'.$j.'_fila_'.$i);
                $unidad_almacen = $this->input->post('unidad_almacen_'.$j.'_fila_'.$i);
                if($caja_almacen == 0 && $paquete_almacen == 0 && $unidad_almacen == 0)
                {
                 // echo "<script language='JavaScript'>alert('No guardo');</script>";                     
                }
                else
                {
                    $data1 = array(         'document_code' => $this->input->post('numero'),
                                            'product_code' => $code_product,
                                            'box' => $caja_almacen,
                                            'pack' => $paquete_almacen,
                                            'unit' => $unidad_almacen,
                                            'id_warehouse' => $j);
                    $this->Shop_kardex_model->addKardexShop($data1);
                    if(count($productos_en_alamcenes)<=0)
                    {                   
                      $res = $this->Warehouses_product_model->add($code_product,$code_almacen,$caja_almacen,$paquete_almacen,$unidad_almacen,0);                   
                     // echo "<script language='JavaScript'>alert('Inserto.$res');</script>";  
                    }
                    else
                    {
                      $res = $this->Warehouses_product_model->add($code_product,$code_almacen,$caja_almacen,$paquete_almacen,$unidad_almacen,1);                   
                    //  echo "<script language='JavaScript'>alert('Actualizo.$res');</script>";                        
                    }  
                }
                 
            }               
               
           }    
        
          $this->add(true); 
	}
        
        else
        {
           $this->load->model('Shop_search_model');
           $this->load->model('Shop_model');
           $this->load->model('Warehouses_product_model');
           $this->load->model('Shop_kardex_model');
           $contenaumentador = $this->input->post('contador_detalles');
           $datei = $this->input->post('date');
           $date = str_replace('/', '-', $datei);
           $datef = date("Y-m-d", strtotime($date) ); 
           $document_code = $this->input->post('numero');           
         
           $data2 = array('document_type_id' => 3,                        
                        'date' => $datef,
                        'id_supplier' =>  $this->input->post('supplier'),
                        'observations' => $this->input->post('observations'),                        
                        'amount' => $this->input->post('subtotalgeneral')
                        );
           
                        $this->Shop_model->update_shop($document_code,$data2);
           
           for($i = 1 ; $i <= $contenaumentador; $i++){
               
               $data3 = array('document_code' => $document_code,
                             'product_code' => $this->input->post('code_product_'.$i.''),
                             'box' => $this->input->post('total_cajas_'.$i.''),  
                             'pack' => $this->input->post('total_paquetes_'.$i.''),  
                             'unit' => $this->input->post('total_unidades_'.$i.''),  
                             'flete' => $this->input->post('total_flete_'.$i.''),  
                             'seguro' => $this->input->post('total_seguro_'.$i.''), 
                             'Ad_Val' => $this->input->post('total_adval_'.$i.''), 
                             'Val_Unit' => $this->input->post('total_price_'.$i.''), 
                             'amount' => $this->input->post('total_subtotalprice_'.$i.''), 
                            );
                $this->Shop_model->add_shop_dateils($data3);
               
            for ($j = 1; $j <= 7; $j++){
                
                 $code_product = $this->input->post('code_product_'.$i);
                 $code_almacen = $this->input->post('code_almacen_'.$j.'_fila_'.$i);
                 //echo "<script language='JavaScript'>alert('code_product.$code_product');</script>";
                // echo "<script language='JavaScript'>alert('code_almacen.$code_almacen');</script>";
                 $productos_en_alamcenes = $this->Shop_model->select_almacen_productos($code_product,$code_almacen);
                 $num = count($productos_en_alamcenes);
                   // echo "<script language='JavaScript'>alert('JavaScript.$num');</script>";
                   // echo "caja almacen".$j." fila ".$i.": ".$this->input->post('caja_almacen_'.$j.'_fila_'.$i)."<br>";
                   // echo "paquete almacen".$j." fila ".$i.": ".$this->input->post('paquete_almacen_'.$j.'_fila_'.$i)."<br>";
                  // echo "unidad almacen".$j." fila ".$i.": ".$this->input->post('unidad_almacen_'.$j.'_fila_'.$i)."<br><br><br>";
                
                $caja_almacen = $this->input->post('caja_almacen_'.$j.'_fila_'.$i);
                $paquete_almacen = $this->input->post('paquete_almacen_'.$j.'_fila_'.$i);
                $unidad_almacen = $this->input->post('unidad_almacen_'.$j.'_fila_'.$i);
                if($caja_almacen == 0 && $paquete_almacen == 0 && $unidad_almacen == 0)
                {
                 // echo "<script language='JavaScript'>alert('No guardo');</script>";                     
                }
                else
                {
                    $data1 = array(         'document_code' => $this->input->post('numero'),
                                            'product_code' => $code_product,
                                            'box' => $caja_almacen,
                                            'pack' => $paquete_almacen,
                                            'unit' => $unidad_almacen,
                                            'id_warehouse' => $j);
                    $this->Shop_kardex_model->addKardexShop($data1);
                    if(count($productos_en_alamcenes)<=0)
                    {                   
                      $res = $this->Warehouses_product_model->add($code_product,$code_almacen,$caja_almacen,$paquete_almacen,$unidad_almacen,0);                   
                     // echo "<script language='JavaScript'>alert('Inserto.$res');</script>";  
                    }
                    else
                    {
                      $res = $this->Warehouses_product_model->add($code_product,$code_almacen,$caja_almacen,$paquete_almacen,$unidad_almacen,1);                   
                    //  echo "<script language='JavaScript'>alert('Actualizo.$res');</script>";                        
                    }  
                }
                 
            }               
               
           }    
        
          $this->index(true);
            
        }
        
        }
        
        
     public function codeguia() {
        $this->load->model('Document_types_model');
        $codeguia = $this->Document_types_model->generanum_shop();

        foreach ($codeguia as $c) {
            $codestring = $c->document_code;
            $num = (int) $codestring + 1;
            echo str_pad((int) $num, 12, "0", STR_PAD_LEFT);
        }
    }
        
      public function search_code_unique() {
        $this->load->model('Shop_model');
        $document_code = $this->input->post('numero');

        echo $this->Shop_model->comparaction_code($document_code);
        return false;
       
      }
    
}
?>
